package com.hb.controller;

import com.hb.entity.UpdateReq;
import com.hb.entity.User;
import com.hb.entity.UserAddress;
import com.hb.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hubin
 * @description
 * @createTime 2024/3/8 14:42
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @PostMapping("/insert")
    public User insert(@RequestBody User user) {
        userMapper.insert(user);
        return user;
    }

    @PostMapping("/batchInsert")
    public List<User> batchInsert(@RequestBody List<User> users) {
        userMapper.batchInsert(users);
        return users;
    }

    @PutMapping("/update")
    public User update(User user) {
        userMapper.updateById(user);
        return user;
    }

    @PutMapping("/batchUpdate")
    public List<User> batchUpdate(@RequestBody List<User> users) {
        userMapper.batchUpdate(users);
        return users;
    }

    @PutMapping("/updatePassword")
    public int updatePassword(@RequestBody UpdateReq updateReq) {
        // todo: 加密密码内容
        return userMapper.updatePassword(updateReq.getIdGt(), updateReq.getPassword());
    }

    @GetMapping("/get/{id}")
    public User get(@PathVariable("id") Long id) {
        return userMapper.selectById(id);
    }

    @GetMapping("/getList")
    public List<User> getList(List<Long> ids) {
        return userMapper.selectList(ids);
    }

    @GetMapping("/getAll")
    public List<User> getAll() {
        return userMapper.selectAll();
    }

    @GetMapping("/getUserAddress")
    public List<UserAddress> getUserAddress() {
        return userMapper.selectUserAddress();
    }

    @GetMapping("/getAllPassword")
    public List<String> getAllPassword() {
        return userMapper.selectAllPassword();
    }

    @GetMapping("/getPasswordById/{id}")
    public String getPasswordById(@PathVariable("id") Long id) {
        return userMapper.selectPasswordById(id);
    }
}
